package com.collect.senegal.zhiScene.zhiAuditer.zhiPerformance;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.collect.senegal.R;
import com.collect.senegal.zhiAdater.zhiAuditerPerfermance.ZHIAuditerPerfermanceMonth;
import com.collect.senegal.zhiBase.BaseFragment;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRAuditerWholeKfMonthBonusEntity;
import com.collect.senegal.zhiBean.zhiSend.ZHISAuditerWholeKfMonthBonusEntity;
import com.collect.senegal.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.senegal.zhiScene.zhiAuditer.zhiRoot.ZHIRootAuditerFragmentInterface;
import com.collect.senegal.zhiScene.zhiWidget.zhiAuditer.ZHIAuditerMonthSelect;
import com.collect.senegal.zhiScene.zhiWidget.zhiDialog.ZHIDialogMessage;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class ZHIAuditerPerfermanceFragment extends BaseFragment<ZHIAuditerPerfermancePresenter> implements ZHIAuditerPerfermanceContract.View {

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.monthAuditerSelect)
    ZHIAuditerMonthSelect monthAuditerSelect;

    @BindView(R.id.textAuditerPerfermanceTotalBonusValue)
    TextView textAuditerPerfermanceTotalBonusValue;

    @BindView(R.id.textAuditerPerfermanceBonusRepaymentValue)
    TextView textAuditerPerfermanceBonusRepaymentValue;

    @BindView(R.id.textAuditerPerfermanceBonusUnPaidValue)
    TextView textAuditerPerfermanceBonusUnPaidValue;

    @BindView(R.id.rcAuditerPerfermanceList)
    RecyclerView rcAuditerPerfermanceList;

    @BindView(R.id.imageAuditerPerfermanceTotalBonusTips)
    ImageView imageAuditerPerfermanceTotalBonusTips;

    @BindView(R.id.llAuditerPerfermanceTotalBonusExt)
    LinearLayout llAuditerPerfermanceTotalBonusExt;

    private String mBonusNum;
    private String mPenaltyNum;

    private ZHIRootAuditerFragmentInterface mZHIRootAuditerFragmentInterface;

    private final ZHIAuditerPerfermanceMonth mZHIAuditerPerfermanceMonth = new ZHIAuditerPerfermanceMonth();

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_perfermance_auditer;
    }

    @Override
    protected void initView(View view) {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

            initEvent();
            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mZHIRootAuditerFragmentInterface = (ZHIRootAuditerFragmentInterface) activity;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @Override
    public void onGetWholeKfMonthBonus(ZHIRAuditerWholeKfMonthBonusEntity zhirAuditerWholeKfMonthBonusEntity) {
        srl_swipe_layout.setRefreshing(false);
        if (zhirAuditerWholeKfMonthBonusEntity != null) {
            mBonusNum = zhirAuditerWholeKfMonthBonusEntity.getBonusSum().getBonusNum();
            mPenaltyNum = zhirAuditerWholeKfMonthBonusEntity.getBonusSum().getPenaltyNum();

            String totalBonus = zhirAuditerWholeKfMonthBonusEntity.getBonusSum().getTotalBonus();
            textAuditerPerfermanceTotalBonusValue.setText(totalBonus);

            String bonusAmount = zhirAuditerWholeKfMonthBonusEntity.getBonusSum().getBonusAmount();
            textAuditerPerfermanceBonusRepaymentValue.setText(bonusAmount);

            String penaltyAmount = zhirAuditerWholeKfMonthBonusEntity.getBonusSum().getPenaltyAmount();
            textAuditerPerfermanceBonusUnPaidValue.setText(penaltyAmount);

            mZHIAuditerPerfermanceMonth.setNewData(zhirAuditerWholeKfMonthBonusEntity.getSingleDayDetail());
        }
    }

    @SuppressLint({"InflateParams", "NotifyDataSetChanged"})
    public void initEvent() {
        try {
            srl_swipe_layout.setOnRefreshListener(() -> initData(false));

            mZHIAuditerPerfermanceMonth.setEmptyView(getRefreshEmptyView());
            mZHIAuditerPerfermanceMonth.setColumnItemClick(item -> {
                ZHIAuditerPerfermanceManager.getInstance().phoneNum = "";
                ZHIAuditerPerfermanceManager.getInstance().type = "";
                ZHIAuditerPerfermanceManager.getInstance().yearAndMonth = monthAuditerSelect.getStartDate();
                ZHIAuditerPerfermanceManager.getInstance().yearAndMonthAndDay = item.getBonusDate();
                ZHIProjectRouter.toAuditerPerfermance();
            });

            rcAuditerPerfermanceList.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
            rcAuditerPerfermanceList.setAdapter(mZHIAuditerPerfermanceMonth);

            monthAuditerSelect.setDateItemClick(date -> initData(true));

            String gContent = "Bonus = Nombre de commandes remboursées * 500 - Nombre de commandes impayées * 400\\n\\nCommandes impayées : Les commandes en retard de >= 8 jours pour les commandes de remboursement, 400 seront remboursés pour chaque commande\\n\\nCommandes de remboursement : Les commandes en retard de <= 7 jours pour les commandes de remboursement, chaque commande sera récompensée 500";//getString(R.string.auditer_bonus_tips_dialog);
            imageAuditerPerfermanceTotalBonusTips.setOnClickListener(view -> ZHIDialogMessage.showDialog(requireActivity(), "", gContent, true, null));

            llAuditerPerfermanceTotalBonusExt.setOnClickListener(view -> {
                ZHIAuditerPerfermanceManager.getInstance().phoneNum = "";
                ZHIAuditerPerfermanceManager.getInstance().type = "ALL";
                ZHIAuditerPerfermanceManager.getInstance().yearAndMonth = monthAuditerSelect.getStartDate();
                ZHIAuditerPerfermanceManager.getInstance().yearAndMonthAndDay = "";
                ZHIProjectRouter.toAuditerPerfermance();
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initData(boolean showLoading) {
        if (mPresenter != null) {
            ZHISAuditerWholeKfMonthBonusEntity zhisAuditerWholeKfMonthBonusEntity = new ZHISAuditerWholeKfMonthBonusEntity();
            zhisAuditerWholeKfMonthBonusEntity.setYearAndMonth(monthAuditerSelect.getStartDate());
            mPresenter.getWholeKfMonthBonus(getActivity(), zhisAuditerWholeKfMonthBonusEntity, showLoading);
        }
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> {
            initData(true);
        });
        return emptyView;
    }

    public void refreshData() {
        initData(true);
    }

}